require(['grid', 'topBar', 'layout','validator', 'select2'], function () {
    var $top = $('#top'),$grid = $('#grid'),reloadGridData, corpId="";
	
 // 左侧树
    commonFun.leftTreeNodeClickObj['sysAlarmCorpIdSet'] = function (event, treeId, treeNode) {
        corpId = treeNode.corpId;
        if (treeNode.level == 0) {
            corpId = "";
        }
    };
    
    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                {
                	text: 'ECU节点',
                    name: 'ecuMftCode',
                    type: 'select',
                    width: 140,
                    dict: 'ECU00000'
                },
                {
                    text: '是否报警',
                    name: 'isAlarm',
                    type: 'select',
                    data: [
                        {text: '是', value: '1',selected:true},
                        {text: '否', value: '0'}
                    ]
                },
                {
                    text: '故障编码',
                    name: 'mftTypeCode',
                    width: '120',
                    type: 'text'
                },
                {
                    text: '故障名称',
                    name: 'mftTypeName',
                    width: '120',
                    type: 'text'
                },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                    	reloadGridData();
                    }
                }

            ],
            right: [
                {
                    text: '过滤报警',
                    type: 'btn',
                    authority: '',
                    click: function () {
                    	setNotAlarm();
                    }
                },
                {
                    text: '解除过滤',
                    type: 'btn',
                    authority: '',
                    click: function () {
                    	setAlarm();
                    }
                }
            ]
        });
    }

    //grid初始化
    function gridInit() {
    	var node = commonFun.getLeftTreeSelectedNodes();
    	if(node && node.length > 0){
    		corpId = node[0].corpId;
    	}
    	
        $grid.grid({
            url: "/alarmPushSetting/searchAlarmFault.htm",
            multiselect: true,
            colModel: [
                {name: "mftDiscCode", label: 'mftDiscCode', hidden: true},
                {name: "corpId", label: 'corpId', hidden: true},
                {name: "mftTypeName", label: '故障名称'},
                {name: "ecuMftName",  label: 'ECU节点'},
                {name: "mftGradeName",  label: '故障等级'},
                {name: "mftTypeCode", label: '故障编码'},
                {name: "isAlarm", label: '是否报警'}
            ]
        });
    }

    //查询grid
	reloadGridData = function(){
		if(corpId == ""){
			$.messager.alert("提示", "请选择左侧企业。", "info");
			return;
		}
		var condition = $top.getValues();
		condition.corpId = corpId;
		 $grid.grid('setGridParam', {
             datatype: "JSON",
             page: 1,
             postData: condition
         }).trigger("reloadGrid");
	};
	
	
	function setNotAlarm(){
		var ids = $grid.grid('getGridParam','selarrrow');
		if(corpId==""){
			$.messager.alert("提示", "请选择左侧企业。", "info");
			return;
		}
		if(ids && ids.length > 0){
			var discs = [], types = [];
			$.each(ids, function(index, id){
				var rowData = $grid.grid('getRowData',id);
				discs.push(rowData.mftDiscCode);
				types.push(rowData.mftTypeCode);
			});
			$.ajaxExt({
				url: "/alarmPushSetting/setAlarmFault.htm",
				data: { 
						"corpId": corpId,
						"mftDiscCodes": discs,
						"mftTypes": types
					  }
			}).done(function(data){
				reloadGridData();
				$.messager.alert("提示", "保存成功。", "info");
			});
		}
	}
	
	function setAlarm(){
		var ids = $grid.grid('getGridParam','selarrrow');
		if(corpId==""){
			$.messager.alert("提示", "请选择左侧企业。", "info");
			return;
		}
		if(ids && ids.length > 0){
			var discs = [], types = [];
			$.each(ids, function(index, id){
				var rowData = $grid.grid('getRowData',id);
				discs.push(rowData.mftDiscCode);
				types.push(rowData.mftTypeCode);
			});
			$.ajaxExt({
				url: "/alarmPushSetting/setNotAlarmFault.htm",
				data: { 
						"corpId": corpId,
						"mftDiscCodes": discs,
						"mftTypes": types
					  }
			}).done(function(data){
				reloadGridData();
				$.messager.alert("提示", "保存成功。", "info");
			});
		}
	}
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
    
});